package cn.gok.server.service.impl;

import cn.gok.common.dao.goods.GoodsCategoryMapper;
import cn.gok.common.entity.backstage.Commons;
import cn.gok.common.entity.backstage.dto.CategoryTree;
import cn.gok.common.entity.goods.GoodsCategory;
import cn.gok.server.service.CategoryService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: DxlinY
 * @apiNote:
 * @date: 2020/11/1
 * @time: 17:19
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override

    public List <CategoryTree> listToTree() {
        List <CategoryTree> result = toTree(Commons.cateGoryParentId);
        log.info("菜单树状结构{}", result);
        return result;
    }

    /**
     * 递归形成树状菜单
     *
     * @param parentId
     * @return
     */
    private List <CategoryTree> toTree(String parentId) {
        List <CategoryTree> result = new ArrayList <>();
        List <GoodsCategory> categories = goodsCategoryMapper.listByParentId(parentId);
        for (GoodsCategory goodsCategory : categories) {
            CategoryTree categoryTree = new CategoryTree(goodsCategory.getCategoryId(), goodsCategory.getCategoryName(), goodsCategory.getCatyDesc(), toTree(goodsCategory.getCategoryId().toString()));
            result.add(categoryTree);
        }
        return result;
    }

    @Override
    public int updateById(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.updateById(goodsCategory);
    }

    @Override
    public PageInfo pageInfo(Object target) {
        return null;
    }

}
